import filters from '@/filters'
import util from '@/libs/util'
import dayjs from 'dayjs'

function formatDate (date) {
  return dayjs(date).format('YYYY-MM-DD')
}
/**
 * 创建[预约工单]判断是否有设备时, 设备数量是否达到上限的值,
 * 其中4无网关门锁不会用到, 因为会被转换成1门锁判断
 */
export const deviceLimit = {
  2: 1, // 门锁上限1个
  3: 4, // 电表上限4个
  4: 1, // 无网关门锁不限
  6: 2, // 水表上限2个
  7: 2 // 水表上限2个
}

// 开始时间三个月后大于结束时间则通过
function isBetweenThreeMonth (start, end) {
  if (!start || !end) return false
  return (
    dayjs(start)
      .add(3, 'M')
      .valueOf() -
            dayjs(end).valueOf() >=
        0
  )
}

function illegalTicketTime (params) {
  const { start_time, end_time, appointment_start_time, appointment_end_time } = params
  const notHasCreateTime = !start_time && !end_time
  const notHasAppointmentTime = !appointment_start_time && !appointment_end_time
  if (notHasCreateTime && notHasAppointmentTime) {
    return '最多导出三个月的工单，请选择预约上门时间范围或创建时间范围'
  }
  return isBetweenThreeMonth(start_time, end_time) || isBetweenThreeMonth(appointment_start_time, appointment_end_time)
    ? false
    : '最多导出三个月的工单'
}

/**
 * 导出工单清单确认
 * @param {*} vm 组件this
 * @param {*} params 导出参数
 * @param {*} cb 确认之后的回调
 * @param {*} type 1 安装 2 售后
 */
export function confirmExport (vm, params, cb, type) {
  const timeStatus = illegalTicketTime(params)
  if (timeStatus) {
    vm.$Message.error({
      content: timeStatus,
      duration: 6
    })
    return
  }
  const stateFilter = type === 1 ? filters.WOInstallStatus : filters.WORepairStatus
  vm.$Modal.confirm({
    title: '导出工单清单',
    onOk: () => {
      cb(params)
    },
    render: h => {
      const appointmentTimerange =
                params.appointment_end_time && params.appointment_start_time
                  ? `${formatDate(params.appointment_start_time)} - ${formatDate(params.appointment_end_time)}`
                  : '全部时间段'
      const timerange =
                params.start_time && params.end_time ? `${formatDate(params.start_time)} - ${formatDate(params.end_time)}` : '全部时间段'
      let differenceOption = null
      if (type === 1) {
        differenceOption = <kv-item label="设备类型:">{filters.deviceType(params.device_type)}</kv-item>
      } else {
        differenceOption = <kv-item label="工单紧急度:">{(params.respond_hour && params.respond_hour + '小时') || '全部'}</kv-item>
      }
      return (
        <kv-wrap label-width="120" align="left" padding="30px 0 0" marginBottom="10px" fontSize="13px">
          <kv-item label="商户名称:">{params.clientName || '全部'}</kv-item>
          <kv-item label="所在城市:">{params.cityName || '全部'}</kv-item>
          <kv-item label="预约上门时间:">{appointmentTimerange}</kv-item>
          <kv-item label="工单状态:">{stateFilter(params.state)}</kv-item>
          {differenceOption}
          {params.sn && <kv-item label="工单编号:">{params.sn || '不限'}</kv-item>}
          {params.address && <kv-item label="房源地址:">{params.address || '不限'}</kv-item>}
          {params.block && <kv-item label="小区:">{params.block || '不限'}</kv-item>}
          <kv-item label="安装师傅:">{params.implementerName || '全部'}</kv-item>
          <kv-item label="创建时间:">{timerange}</kv-item>
        </kv-wrap>
      )
    }
  })
}

export function confirmBackModal (vm, ticket, cb) {
  let description = ''
  vm.$Modal.confirm({
    title: '打回客服',
    loading: true,
    render: h => (
      <kv-wrap label-width="80" align="left" padding="30px 0 0" marginBottom="20px" fontSize="13px">
        <kv-item label="工单编号:">{ticket.ticket_sn}</kv-item>
        <kv-item label="房源地址:">{util.concatLocation(ticket.home_info)}</kv-item>
        <i-input
          type="textarea"
          value={description}
          placeholder="打回原因(必填)"
          onInput={val => {
            description = val
          }}
        />
      </kv-wrap>
    ),
    onOk: function () {
      if (!description) {
        vm.$Message.info('请填写原因')
        this.buttonLoading = false
      } else {
        cb(ticket.id, description)
      }
    }
  })
}
